<?php
 /*
   Plugin Name: HMD Picasa photos album
   Version: 1.0
   Description: Get all photos for your public picasa album. In order to insert your post, insert a shortcode as [[hmd_album id=userid]]. For example [[hmd_album id=dung14000]]. You can see a demo <a href="http://www.hoangmanhdung.com/album">here</a>
   Author: Hoang Manh Dung
   Author URI: http://www.hoangmanhdung.com
   Plugin URI: http://www.hoangmanhdung.com
   */
  
  /*
   Copyright 2008  Hoang Manh Dung  (email : dung14000@gmail.com)
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
$plugin_url = WP_PLUGIN_URL .'/hmd-picasa';
add_action('wp_head', 'insert_asset');
add_filter('the_content','album_filter');
function get_content($userId){
	global $plugin_url;
	$doc = new DOMDocument();
	$doc->load( 'http://picasaweb.google.com/data/feed/api/user/'. $userId .'?kind=album');
	$entrys = $doc->getElementsByTagName("entry");
	$content = '<div class="hmd_album_container">';
	foreach( $entrys as $entry)  {
		$thumbnail = $entry->getElementsByTagNameNS('http://search.yahoo.com/mrss/','thumbnail');
		$thumbUrl = $thumbnail->item(0)->getAttribute('url');
		$albumFeed = $entry->getElementsByTagName('link')->item(0)->getAttribute('href');
		$content .= '<div class="hmd_album">'; 
		$content .=  '	<a href="'. $albumFeed .'" class="hmd_link"><img src="'.$thumbUrl.'"/></a>';
		$content .=  '</div>';
	}
	$content .=  '</div>';
	unset($doc);
	return $content;
}

function album_filter($content){
	global $plugin_url;
	$pattern = '/\[{2}hmd_album\s+id=.+\]{2}/i';
	preg_match_all($pattern, $content, $arr, PREG_PATTERN_ORDER);
	foreach($arr[0] as $key => $value){
		$userId =preg_replace('/\[*|\]*|hmd_album|\s*|id=/i','',$value);
		$new_content = get_content($userId);
		$content = str_replace($value, $new_content, $content);
	}
	$content .= '<div id="hmd_image_wrapper"></div>';
	$content .= '<script src="'.$plugin_url.'/script.php"  type="text/javascript"></script>';
	return $content;
}

function insert_asset(){
	global $plugin_url;
	echo '<script src="'.$plugin_url.'/js/jquery-1.4.4.min.js" type="text/javascript" charset="utf-8"></script>';
	echo '<link rel="stylesheet" href="'.$plugin_url.'/css/prettyPhoto.css" type="text/css" />'; 
	echo '<link rel="stylesheet" href="'.$plugin_url.'/css/style.css" type="text/css" />';
	echo '<script src="'.$plugin_url.'/js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>';
	echo '<script type="text/javascript">var WP_PLUGIN_URL = "'. $plugin_url .'"</script>';
}
?>
